/**
 *====================================================
 * 文件名称: DetainedApplicationServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.DetainedApplicationEntity;
import com.yuexunit.finance.project.entity.lexicon.ApplicationSimpleStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: DetainedApplicationServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(DetainedApplicationService.class)
public class DetainedApplicationServiceImpl implements DetainedApplicationService {

	@Override
	public Long createDetainedApplication(DetainedApplicationEntity entity) {
		entity.setApplicationState(ApplicationSimpleStateEnum.NOT_APPROVE);
		entity.setApplicantDateTime(DateUtil.now());
		entity.setDetainedNumber(SettingConstants.PRE_NUMBER_DETAINED + RandomUtil.generateTimeNumber());
		PersistUtil.insert(entity);
		return entity.getDetainedId();
	}

	@Override
	public void updateDetainedApplication(DetainedApplicationEntity entity) {
		DetainedApplicationEntity persist = inquireDetainedApplication(entity.getDetainedId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	@Transactional
	public void renovateDetainedApplication(DetainedApplicationEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public DetainedApplicationEntity inquireDetainedApplication(Long detainedId) {
		return PersistUtil.get(DetainedApplicationEntity.class, detainedId);
	}

	@Override
	public PageList<DetainedApplicationEntity> inquireDetainedApplicationPageList(Long[] detainedIds, Long financeId, Long enterpriseId, ApplicationSimpleStateEnum applicationState, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DetainedApplicationEntity.class);
		if (detainedIds != null) {
			criteria.add(Restrictions.in("detainedId", detainedIds));
		}
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (applicationState != null) {
			criteria.add(Restrictions.eq("applicationState", applicationState));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<Long> countWaitDetainedApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(DetainedApplicationEntity.class);
		criteria.add(Restrictions.eq("applicationState", ApplicationSimpleStateEnum.NOT_APPROVE));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("detainedId")));
		return PersistUtil.get(criteria);
	}

}
